package top.xin1901.j2ecd.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import top.xin1901.j2ecd.beans.Permission;
import top.xin1901.j2ecd.common.response.R;
import top.xin1901.j2ecd.service.PermissionService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author HeYunjia
 */

@RestController
@CrossOrigin("*")
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    PermissionService permissionService;

    @PostMapping
    public R postPermission(@RequestBody Permission permission) {
        return R.success()
                .add("permission", permissionService.insertOne(permission));
    }

    @DeleteMapping
    public R deletePermission() {
        return null;
    }

    @PutMapping
    public R putPermission() {
        return null;
    }

    @GetMapping
    public R getPermissionAll() {
        return R.success()
                .add("permissions", permissionService.selectAll());
    }

    @GetMapping("/{pageNumber}/{pageSize}")
    public R getPermissionPage(@PathVariable Integer pageNumber,
                         @PathVariable Integer pageSize) {

        PageHelper.startPage(pageNumber, pageSize);

        List<Permission> lists = permissionService.selectAll();

        return R.success()
                .add("page", new PageInfo<>(lists));
    }

    @GetMapping("/{permissionId}")
    public R getPermissionOne(@PathVariable Long permissionId) {
        return R.success()
                .add("permission", permissionService.selectOne(permissionId));
    }

    @GetMapping("type/{typeId}")
    public R getPermissionOfType(@PathVariable Long typeId) {
        return R.success()
                .add("permissions", permissionService.selectByTypeId(typeId));
    }
}
